/*
 * @(#)LockManager.java   09/11/09
 *
 * Copyright (c) 2009 HKUST. All rights reserved.
 *
 */

package org.ust.downloader;

//~--- JDK imports ------------------------------------------------------------

import java.util.Iterator;
import java.util.Vector;

/**
 * 
 * 
 * @version 0.1, 09/11/09
 * @author Nelson Lau
 */
public class LockManager
{
	private Vector < NavigationLock >	locks;

	/**
	 *
	 */
	public LockManager ( )
	{
		locks = new Vector < NavigationLock > ( );
	}

	/**
	 * 
	 * 
	 * @return
	 */
	public Vector < NavigationLock > getLocks ( )
	{
		return locks;
	}

	/**
	 * 
	 * 
	 * @param lock
	 * 
	 * @return
	 */
	public boolean addLock ( NavigationLock lock )
	{
		return locks.add ( lock );
	}

	/**
	 * 
	 * 
	 * @param index
	 * 
	 * @return
	 */
	public NavigationLock getLockAt ( int index )
	{
		return locks.elementAt ( index );
	}

	/**
	 * 
	 * 
	 * @return
	 */
	public Vector < NavigationLock > getAbortedLocks ( )
	{
		Vector < NavigationLock > r = new Vector < NavigationLock > ( );

		for ( Iterator < NavigationLock > iterator = locks.iterator ( ) ; iterator
				.hasNext ( ) ; )
		{
			NavigationLock navigationLock = ( NavigationLock ) iterator.next ( );

			if ( navigationLock.isAbort ( ) )
			{
				r.add ( navigationLock );
			}
		}

		return r;
	}

	/**
	 * 
	 * 
	 * @return
	 */
	public Vector < NavigationLock > getUnabortedLocks ( )
	{
		Vector < NavigationLock > r = new Vector < NavigationLock > ( );

		for ( Iterator < NavigationLock > iterator = locks.iterator ( ) ; iterator
				.hasNext ( ) ; )
		{
			NavigationLock navigationLock = ( NavigationLock ) iterator.next ( );

			if ( ! navigationLock.isAbort ( ) )
			{
				r.add ( navigationLock );
			}
		}

		return r;
	}

	/**
	 * 
	 * 
	 * @return
	 */
	// public boolean noRunningFilePageLocks ( )
	// {
	// for ( Iterator < NavigationLock > iterator = locks.iterator ( ) ;
	// iterator
	// .hasNext ( ) ; )
	// {
	// NavigationLock lock = iterator.next ( );
	//
	// // detect running lock
	// if ( ! lock.isFinished ( ) && ! lock.isAbort ( )
	// && ( lock instanceof FilePageNavigationLock ) )
	// {
	// return false;
	// }
	// }
	//
	// return true;
	// }

	// public int getUnabortedFilePageNavigationLockSize ( )
	// {
	// int unabortedCount = 0;
	// for ( NavigationLock lock : locks )
	// {
	// if ( ! lock.isAbort ( ) && lock instanceof FilePageNavigationLock )
	// {
	// unabortedCount++ ;
	// }
	// }
	// return unabortedCount;
	// }
	//
	// public int getTotalFilePageLockSize ( )
	// {
	// int c = 0;
	//
	// for ( Iterator < NavigationLock > iterator = locks.iterator ( ) ;
	// iterator
	// .hasNext ( ) ; )
	// {
	// NavigationLock lock = iterator.next ( );
	//
	// if ( lock instanceof FilePageNavigationLock )
	// {
	// c++ ;
	// }
	// }
	//
	// return c;
	// }

	/**
	 * 
	 * 
	 * @return
	 */
	public boolean noRunningLocks ( )
	{
		for ( Iterator < NavigationLock > iterator = locks.iterator ( ) ; iterator
				.hasNext ( ) ; )
		{
			NavigationLock lock = iterator.next ( );

			if ( ! lock.isAbort ( ) && ! lock.isFinished ( )
					&& ! ( lock instanceof DownloadLock ) )
			{
				return false;
			}
		}
		return true;
	}

	// public int getUnabortedProjectListLockSize ( )
	// {
	// int unabortedCount = 0;
	// for ( NavigationLock lock : locks )
	// {
	// if ( ! lock.isAbort ( )
	// && lock instanceof ProjectListNavigationLock )
	// {
	// unabortedCount++ ;
	// }
	// }
	// return unabortedCount;
	// }

	// public int getTotalProjectListLockSize ( )
	// {
	// int c = 0;
	//
	// for ( Iterator < NavigationLock > iterator = locks.iterator ( ) ;
	// iterator
	// .hasNext ( ) ; )
	// {
	// NavigationLock lock = iterator.next ( );
	//
	// if ( lock instanceof ProjectListNavigationLock )
	// {
	// c++ ;
	// }
	// }
	//
	// return c;
	// }

	/**
	 * 
	 * 
	 * @return
	 */
	// public boolean isAllLocksFinished ( )
	// {
	// int count = locks.size ( );
	// int c = 0;
	//
	// for ( Iterator < NavigationLock > iterator = locks.iterator ( ) ;
	// iterator
	// .hasNext ( ) ; )
	// {
	// NavigationLock lock = iterator.next ( );
	//
	// if ( lock.isFinished ( ) )
	// {
	// c++ ;
	// }
	// }
	//
	// return c == count;
	// }

	/**
	 * 
	 * 
	 * @return
	 */
	// public boolean noRunningHomepageLocks ( )
	// {
	// int abortedCount = 0;
	//
	// for ( Iterator < NavigationLock > iterator = locks.iterator ( ) ;
	// iterator
	// .hasNext ( ) ; )
	// {
	// NavigationLock lock = iterator.next ( );
	//
	// if ( ! lock.isFinished ( ) && ! lock.isAbort ( )
	// && ( lock instanceof HomepageNavigationLock ) )
	// {
	// abortedCount++ ;
	// }
	// }
	//
	// return abortedCount == 0;
	// // int unabortedCount = getUnabortedHomepageLockSize ( );
	// // int total = getTotalHomepageLockSize ( );
	// //
	// // return ( abortedCount + unabortedCount == total ) && ! isZero;
	// }

	// public int getTotalHomepageLockSize ( )
	// {
	// int total = 0;
	// for ( NavigationLock lock : locks )
	// {
	// if ( lock instanceof HomepageNavigationLock )
	// {
	// total++ ;
	// }
	// }
	// return total;
	// }

	// public int getUnabortedHomepageLockSize ( )
	// {
	// int unabort = 0;
	// for ( NavigationLock lock : locks )
	// {
	// if ( lock instanceof HomepageNavigationLock && ! lock.isAbort ( ) )
	// {
	// unabort++ ;
	// }
	// }
	// return unabort;
	// }

	/**
	 * 
	 * 
	 * @return
	 */
	// public boolean noRunningDownloadLocks ( )
	// {
	// int abortedCount = 0;
	//
	// for ( Iterator < NavigationLock > iterator = locks.iterator ( ) ;
	// iterator
	// .hasNext ( ) ; )
	// {
	// NavigationLock lock = iterator.next ( );
	//
	// if ( ! lock.isFinished ( ) && ! lock.isAbort ( )
	// && ( lock instanceof DownloadLock ) )
	// {
	// abortedCount++ ;
	// }
	// }
	// return abortedCount == 0;
	//
	// // int unabortedCount = getUnabortedDownloadLockSize ( );
	// // int total = getTotalDownloadLockSize ( );
	// //
	// // return ( abortedCount + unabortedCount == total ) && ! isZero;
	// }

	// private int getUnabortedDownloadLockSize ( )
	// {
	// int r = 0;
	// for ( NavigationLock lock : locks )
	// {
	// if ( ! lock.isAbort ( ) && lock instanceof DownloadLock )
	// {
	// r++ ;
	// }
	// }
	// return r;
	// }
	//
	// private int getTotalDownloadLockSize ( )
	// {
	// int c = 0;
	//
	// for ( Iterator < NavigationLock > iterator = locks.iterator ( ) ;
	// iterator
	// .hasNext ( ) ; )
	// {
	// NavigationLock lock = iterator.next ( );
	//
	// if ( lock instanceof DownloadLock )
	// {
	// c++ ;
	// }
	// }
	//
	// return c;
	// }
}
